package com.imaker.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imaker.dao.BaseDao;
import com.imaker.domain.OperateInfo;
import com.imaker.domain.Page;
import com.imaker.field.StaticField;

 /**
 * <p>程序名称：       OperateService.java</p>
 * <p>程序说明：       TODO</p>
 * <p>版权信息：       Copyright mayonghua</p>
 * 
 * @author：          mayonghua
 * @version：         Ver 0.1
 */
@Service
public class OperateService implements IService {
	private BaseDao<OperateInfo> operateDAO = null;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		operateDAO = new BaseDao<OperateInfo>(sessionFactory,OperateInfo.class);
	}
	
	public String save(OperateInfo operateInfo){
		try{
			operateDAO.save(operateInfo);	
		}catch(Exception e){
			e.printStackTrace();
			return StaticField.error;
		}
		return StaticField.success;
	}
	
	/**
	 * 查询所有
	 * @return
	 * @throws Exception
	 */
	public List<OperateInfo> findAll() throws Exception{
		return operateDAO.findAll();
	}
	
	/**
	 * 根据ID进行查找
	 * @param operateInfo
	 * @return
	 * @throws Exception
	 */
	public OperateInfo findById(String id) throws Exception{
		return operateDAO.get(id);
	}
	
	/**
	 * 删除
	 */
	public String delete(List idList){
		try {
			operateDAO.deleteByIdList(idList);
		} catch (Exception e) {
			e.printStackTrace();
			return StaticField.error;
		}
		return StaticField.success;
	}
	
	/**
	 * 更新
	 */
	public String update(OperateInfo operateInfo){
		try {
			operateDAO.update(operateInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return StaticField.error;
		}
		return StaticField.success;
	}
	
	/**
	 * 分页查找
	 */
	public Page listByPage(Page page) throws Exception{
		String hql = "from OperateInfo d";
		return operateDAO.pagination(page, hql," d.createDate");
	}
	
	/**
	 * 分页查找子部门
	 */
	public Page listByPage(Page page,String id) throws Exception{
		String hql = "from OperateInfo o where o.pid = ?";
		return operateDAO.pagination(page, hql," o.createDate",id);
	}
}
